package net.hekka.canyoutalk.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.hekka.canyoutalk.share.property.RequestParameterProperties;
import net.hekka.canyoutalk.web.processor.LogRegisterProcessor;

/**
 *
 * @author jeffreyha
 */
public class LogRegisterServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        HttpSession httpSession = request.getSession(true);
        System.setProperty("file.encoding", "utf-8");
        request.setCharacterEncoding("utf-8");

        int returnValue = 0;

        try {
            // Request 객체에서 Parameters를 구하여 Properties를 생성한다.
            RequestParameterProperties requestParameterProperties = getRequestParameterProperties(request);

            LogRegisterProcessor logRegisterProcessor = new LogRegisterProcessor();
            returnValue = logRegisterProcessor.registerTempLog(requestParameterProperties);
        } finally {
            writeReturnValue(response, returnValue);
        }
    }

    /**
     * Requst객체의 Parameters의 값을 저장한 Properties객체를 구한다.
     * @param httpServletRequest Request객체
     * @return Requst객체의 Parameters의 값을 저장한 Properties객체
     */
    private RequestParameterProperties getRequestParameterProperties(HttpServletRequest httpServletRequest) {
        RequestParameterProperties requestParameterPropeties = new RequestParameterProperties();
        Enumeration params = httpServletRequest.getParameterNames();
        while (params.hasMoreElements()) {
            String paramName = (String) params.nextElement();
            String paramValue = httpServletRequest.getParameter(paramName);
            requestParameterPropeties.put(paramName, paramValue);
        }
        return requestParameterPropeties;
    }

    private void writeReturnValue(HttpServletResponse response, int returnValue) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(returnValue);
		out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
